package com.xunker.dictmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunker.appointment.exception.AppointmentException;
import com.xunker.appointment.model.dictory.DictManager;
import com.xunker.appointment.result.ResultCodeEnum;
import com.xunker.appointment.vo.hospital.ProAndCityVo;
import com.xunker.dictmanager.mapper.DictManagerMapper;
import com.xunker.dictmanager.service.DictManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DictManagerServiceImpl extends ServiceImpl<DictManagerMapper, DictManager> implements DictManagerService {
    @Override
    @Cacheable(value = "dict", key = "#id")//将数据缓存到<findChild,dict>键值对中
    public List<DictManager> findChildData(Long id) {
        QueryWrapper<DictManager> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        wrapper.orderByAsc("id");
        List<DictManager> dictManagers = baseMapper.selectList(wrapper);
        //设置hasChildren字段
        dictManagers.forEach(p -> {
            p.setHasChildren(this.isChildren(p.getId()));
        });
        return dictManagers;
    }

    @Override
    @Cacheable(value = "getName", key = "#dictoryCode+#value")
    public String getName(String dictoryCode, String value) {
        if (StringUtils.isEmpty(dictoryCode)) {
            QueryWrapper<DictManager> wrapper=new QueryWrapper<>();
            wrapper.eq("value",value);
            final DictManager dictManager = baseMapper.selectOne(wrapper);
            return dictManager.getName();
        }else {
            QueryWrapper<DictManager> wrapper=new QueryWrapper<>();
            wrapper.eq("dictory_code",dictoryCode).select("id");
            final List<DictManager> dictManagers = baseMapper.selectList(wrapper);
            if (dictManagers.isEmpty()){
                throw new AppointmentException(ResultCodeEnum.PARAM_ERROR);
            }
            List<Long> pids=new ArrayList<>();
            dictManagers.stream().forEach(s->pids.add(s.getId()));
            //cx子节点
            return baseMapper.selectOne(new QueryWrapper<DictManager>().in("parent_id", pids).eq("value", value)).getName();
        }
    }

    @Override
    public String getName(String value) {
        QueryWrapper<DictManager> wrapper=new QueryWrapper<>();
        wrapper.eq("value",value);
        final DictManager dictManager = baseMapper.selectOne(wrapper);
        return dictManager.getName();
    }

    @Override
    @Cacheable(value = "ProAndCity", key = "#dictoryCode+#value")
    public ArrayList<ProAndCityVo> ProAndCity(String dictoryCode, String value) {
        ArrayList<ProAndCityVo> proAndCityVos =new ArrayList<>();
        List<DictManager> dictManagers;
        if (!"none".equals(dictoryCode)) {
            value = baseMapper.selectOne(new QueryWrapper<DictManager>().eq("dictory_code", dictoryCode).select("id")).getId().toString();
        }else {
            value = baseMapper.selectOne(new QueryWrapper<DictManager>().eq("value", value).select("id")).getId().toString();
        }
        dictManagers = baseMapper.selectList(new QueryWrapper<DictManager>().eq("parent_id", value).select("id,parent_id,name,value"));
        //对象转换函数
        dictManagers.stream().forEach(s->{
            ProAndCityVo proAndCityVo = new ProAndCityVo();
            BeanUtils.copyProperties(s,proAndCityVo);
            proAndCityVos.add(proAndCityVo);
        });
        return proAndCityVos;
    }

    //判断id下面是否有子节点
    private boolean isChildren(Long id) {
        QueryWrapper<DictManager> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }
}
